<template>
  <div class="department-container">
    <div class="app-container">
      <el-card>
        <!-- 用一个行列布局 -->
        <!-- row是控制行数， col是控制列数 -->
        <el-row type="flex" justify="space-between" align="middle" style="height: 40px">
          <el-col :span="20">
            <svg-icon icon-class="home" /><span>江苏传智播客教育科技股份有限公司</span>
          </el-col>
          <el-col :span="4">
            <el-row type="flex" justify="end">
              <!-- 两个内容 -->
              <el-col>负责人</el-col>
              <!-- 下拉菜单 element -->
              <el-col>
                <el-dropdown>
                  <span>
                    操作<i class="el-icon-arrow-down" />
                  </span>
                  <el-dropdown-menu slot="dropdown">
                    <el-dropdown-item @click.native="hAdd">添加子部门</el-dropdown-item>
                  </el-dropdown-menu>
                </el-dropdown>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
        <el-tree
          :data="list"
        >
          <template #default="{data}">
            <el-row
              type="flex"
              justify="space-between"
              align="middle"
              style="height: 40px;width:100%"
            >
              <el-col :span="20">
                <svg-icon icon-class="home" /><span>{{ data.name }}</span>
              </el-col>
              <el-col :span="4">
                <el-row type="flex" justify="end">
                  <!-- 两个内容 -->
                  <el-col>{{ data.manager }}</el-col>
                  <!-- 下拉菜单 element -->
                  <el-col>
                    <el-dropdown>
                      <span>
                        操作<i class="el-icon-arrow-down" />
                      </span>
                      <el-dropdown-menu slot="dropdown">
                        <el-dropdown-item @click.native="hAdd(data.id)">添加子部门</el-dropdown-item>
                        <el-dropdown-item @click.native="hEdit(data.id)">编辑部门</el-dropdown-item>
                        <el-dropdown-item v-if="data.children.length===0" @click.native="hDel(data.id)">删除部门</el-dropdown-item>

                      </el-dropdown-menu>
                    </el-dropdown>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-tree>
      </el-card>

      <el-dialog
        :id="checkId"
        :title=" isEdit? '编辑':'添加'"
        :visible.sync="showDialog"
        :is-edit="isEdit"
      >
        <my-son />
      </el-dialog>
    </div>
  </div>

</template>
<script>

import mySon from '@/views/departments/depDialog.vue'
import { departmentFinddata } from '@/api/departments'
import { fn } from '@/utils/index'
export default {
  components: {
    mySon
  },
  data() {
    return {
      showDialog: false,
      list: [],
      isEdit: false,
      checkId: ''
    }
  },
  created() {
    this.loadDepartData()
  },
  methods: {
    // 加载当前页面数据
    async loadDepartData() {
      try {
        const res = await departmentFinddata()
        res.data.depts.shift()
        this.list = fn(res.data.depts)
        // console.log(res)
      } catch (error) {
        console.log(error)
      }
    },
    hAdd(id) {
      this.showDialog = true
      this.isEdit = false
      this.checkId = id
    },
    hEdit(id) {
      this.showDialog = true
      this.isEdit = true
      this.checkId = id
    },
    hDel(id) {

    }
  }
}
</script>
